package com.bobby.dao.impl;

import com.bobby.dao.BaseDao;
import com.bobby.dao.BookDao;
import com.bobby.pojo.Book;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description TODO
 * @Date 2020/8/14 15:35
 * @Created wenjunpei
 */
public class BookDaoImpl extends BaseDao<Book> implements BookDao {


    @Override
    public int addBook(Book book) throws SQLException {
        String sql = "insert into t_book(name,author,price,sales,stock,img_path) values(?,?,?,?,?,?)";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) throws SQLException {
        String sql = "delete from t_book where id = ?";
        return update(sql,id);
    }

    @Override
    public int updateBook(Book book) throws SQLException {
        String sql = "update t_book set name = ?, author = ? ,price = ?,sales = ?, stock = ? ,img_path = ? where id = ?";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) throws SQLException {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book where id = ?";
        return queryForOne(sql,id);
    }

    @Override
    public List<Book> queryBooks() throws SQLException {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book";
        return queryForList(sql);
    }

    @Override
    public Long queryForPageTotalCount() throws SQLException {
        String sql = "select count(*) from t_book";
        return (Long) queryForSingleValue(sql);
    }

    @Override
    public List<Book> queryForItems(Long begin, Long pageSize) throws SQLException {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book limit ?, ?";
        return queryForList(sql, begin ,pageSize);
    }

    @Override
    public Long queryForPageTotalCountByPrice(Double min, Double max) throws SQLException {
        String sql = "select count(*) from t_book where price between ? and ?";
        return (Long) queryForSingleValue(sql,min,max);
    }

    @Override
    public List<Book> queryForItemsByPrice(Long begin, Long pageSize, Double min, Double max) throws SQLException {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book where price between ? and ? order by price limit ?, ?";
        return queryForList(sql,min,max, begin ,pageSize);
    }
}
